<template>
  <div class="login">
     <div class="login_div">
       <div class="img_div">
          <img src="@/assets/logo.png" alt="" class="login_logo">
       </div>
       <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" status-icon label-width="0" class="login_form">
         <el-form-item prop="username">
           <el-input placeholder="请输入账号"
                     prefix-icon="el-icon-user" v-model.trim="loginForm.username" ></el-input>
         </el-form-item>
         <el-form-item prop="password">
           <el-input placeholder="请输入密码"
                     prefix-icon="el-icon-lock" v-model.trim="loginForm.password" show-password ></el-input>
         </el-form-item>
         <el-form-item class="btns">
           <el-button type="primary" @click="submitForm">登录</el-button>
           <el-button type="info" @click="resetLoginForm">重置</el-button>
         </el-form-item>
       </el-form>
     </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 10, message: '账号长度在5到10之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '密码长度在5到10之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    submitForm() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post('login', this.loginForm)
        if(res.meta.status === 200) {
          this.$message({
            showClose: true,
            message: '恭喜你，登录成功！！！',
            type: 'success'
          })
          window.sessionStorage.setItem('token',res.data.token)
          this.$router.push('/home')
        } else {
          this.$message({
            showClose: true,
            message: '很遗憾，登录失败！！！',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .login {
    background-color: #2b4b6b;
    height: 100%;
  }
  .login .login_div {
    width: 550px;
    height: 400px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-40%);
  }
  .login .login_div .img_div {
    background-color: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
  }
  .login .login_div .login_logo {
    background-color: #eee;
    border-radius: 50%;
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 40px 30px 40px;
    box-sizing: border-box;
  }
  .btns {
    /*display: flex;*/
    /*justify-content: flex-end;*/
    float: right;
  }
</style>
